<template>
	<view class="cu-form-group">
		<view class="grid col-4 grid-square flex-sub">
			<view class="bg-img" v-for="(item, index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
				<image :src="imgList[index]" mode="aspectFill"></image>
				<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
					<text class='cuIcon-close'></text>
				</view>
			</view>
			<view class="solids" @tap="ChooseImage" v-if="imgList.length < imgNum">
				<text class='cuIcon-cameraadd'></text>
			</view>
		</view>
	</view>
</template>

<script>
	import { upload } from "@/common/upload.js";
	export default {
		name: 'MyImgUploadGroup',
		data() {
			return {
				imgNum: parseInt(this.num)
			};
		},
		computed: {
			imgList: {
				get() {
					return this.value;
				},
				set(val) {
					console.log(val);
					this.$emit('input', val)
				}
			}
		},
		props: {
			num: {
				type: String,
				default: ''
			},
			value: {
				type: Array,
				default: () => []
			}
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: this.imgNum, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (chooseRes) => {
						upload(chooseRes.tempFilePaths[0]).then(res => {
							this.imgList = this.imgList.concat(res.fullurl);
						})
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.fullurl
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除图片',
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList = this.imgList.filter((item, index) =>  index != e.currentTarget.dataset.index);
						}
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.img-upload-item {
		display: inline-block;
		padding: 10px 20px 0 0;
		position: relative;

		.cuIcon-add {
			box-sizing: border-box;
			display: block;
			width: 60px;
			height: 60px;
			line-height: 60px;
			text-align: center;
			border: 1px dashed #DDDDDD;
			font-size: 30px;
			color: #aaa;
		}

		.cuIcon-close {
			position: absolute;
			right: 20px;
			top: 10px;
			width: 18px;
			height: 18px;
			line-height: 18px;
			border-radius: 2px;
			text-align: center;
			background-color: #00000060;
			color: #FFFFFF;
			z-index: 999;
		}

		.img {
			display: block;
			width: 60px;
			height: 60px;
		}

		.label {
			display: block;
			line-height: 25px;
			font-size: 12px;
			text-align: center;
			color: #aaa;
		}
	}
</style>
